package com.netflix.service.user.controller;

import com.basic.pojo.good.ShoppingItem;
import com.basic.pojo.user.User;
import com.google.common.collect.ImmutableList;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.basic.controller.RefreshAppsController;
import com.netflix.discovery.DiscoveryClient;
import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.shared.Application;
import com.netflix.discovery.shared.Applications;
import com.netflix.service.user.rpc.ShoppingRpcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.eureka.CloudEurekaClient;
import org.springframework.cloud.util.ProxyUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    RestTemplate restTemplate;

    @Autowired
	ShoppingRpcService shoppingRpcService;

    @RequestMapping(value = "/users", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public List<User> getUsers() {
        User user = new User();
        user.setId(1);
        user.setName("jason");
        user.setSex(1);
        return ImmutableList.of(user);
    }

	@RequestMapping(value = "/user/shopping/items", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public List<ShoppingItem> getUserShoppingItems() {
		List<ShoppingItem> allItems = shoppingRpcService.getAllItems();
		return allItems;
	}

    @RequestMapping(value = "/shopping/items", method = RequestMethod.GET)
    public Object getUserShoppingItems(@RequestParam("userId") int userId) {
        Object response = restTemplate.getForObject("http://shopping-service/shopping/items", List.class);
        return response;
    }

}
